package rpcserver

import (
	rpcv1 "gluenet/internal/agent/api/rpc/v1"
	"gluenet/internal/agent/depend"
	"gluenet/internal/agent/driver/rpcserver"
	"gluenet/internal/agent/service/dagsvc"
	"gluenet/utils/rpc/v2/define"
)

const (
	dag = rpcv1.RPC_SERVICE_DAG
)

func init() {
	depend.Provide(NewDagRouter)
	registerRouter(dag, func(c rpcserver.IRoutes) {
		depend.Invoke(func(d *dagRouter) {
			d.initRouter(c)
		})
	})
}

type dagRouter struct {
	dag *dagsvc.Service
}

func NewDagRouter(svc *dagsvc.Service) *dagRouter {
	return &dagRouter{svc}
}

func (r *dagRouter) initRouter(c rpcserver.IRoutes) {
	c.Group(`dag`).
		Func(`service_map`, r.dagServiceMap).
		Func(`response`, r.dagResponse)
}

func (r *dagRouter) dagServiceMap(c *rpcserver.Context) {
	dagServiceMapReq := new(define.DagServiceMap)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{dagServiceMapReq}); err != nil {
		return
	}
	resp := r.dag.DagServiceMap(*dagServiceMapReq)
	c.JSON(resp)
}

func (r *dagRouter) dagResponse(c *rpcserver.Context) {
	dagResponseReq := new(define.DagResponse)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{dagResponseReq}); err != nil {
		return
	}
	resp := r.dag.DagResponse(*dagResponseReq)
	c.JSON(resp)
}
